import javafx.collections.FXCollections;
import org.omg.DynamicAny._DynStructStub;

import java.util.ArrayList;

/**
 * Created by tom on 10/01/14.
 */
public class PrimeFactors
{
    public static void main(String[] args)
    {
        int inputNumber = 124;

        determinePrimeFactors(inputNumber);

    }

    private static void determinePrimeFactors(int inputNumber)
    {
        ArrayList<Integer> listOfPrimeFactorsForInputNumber = new ArrayList<Integer>();

        ArrayList<Integer> listOfPrimes = new ArrayList<Integer>();
        // place the first prime number into the list
        listOfPrimes.add(2);
        listOfPrimes.add(3);


        int indexOfCurrentPrimeInList =0;

        while( inputNumber >1 )
        {


            int workingPrime = listOfPrimes.get(indexOfCurrentPrimeInList);
            if(inputNumber % workingPrime == 0)
            {
                listOfPrimeFactorsForInputNumber.add(workingPrime);
                inputNumber = (int)(inputNumber / workingPrime*1.0);
                indexOfCurrentPrimeInList=0;
                System.out.println(inputNumber);
            }
            else
            {
                indexOfCurrentPrimeInList++;
                if(indexOfCurrentPrimeInList >= listOfPrimes.size())
                {
                    listOfPrimes.add(calculateNextPrime(workingPrime));
                }
            }

        }

        System.out.println(listOfPrimeFactorsForInputNumber.toString());

    }

    private static int calculateNextPrime(int currentLargestPrime)
    {
        int nextPrimeCandidate = currentLargestPrime+2;

        while (! isPrime(nextPrimeCandidate))
        {
            nextPrimeCandidate+=2;
        }
        return nextPrimeCandidate;
    }

    private static boolean isPrime(int candidate)
    {

        for(int x =2; x < candidate; x++)
        {
            if(candidate%x==0)
            {
                return false;
            }
        }
        return true;
    }
}



